package com.three.digit.line;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.three.digit.algorithm.IAlgorithm;
import com.three.digit.group.IGroup;
import com.three.digit.model.PercentHashSet;
import com.three.digit.model.Point;
import com.three.digit.tools.Utils;

public class TwoNearNumberLine implements IAlgorithm
{
    private Map <String, PercentHashSet <Integer>> serials;

    private IGroup iGroup;

    public TwoNearNumberLine (IGroup group)
    {
        this.iGroup = group;
        this.serials = new HashMap <String, PercentHashSet <Integer>> ();
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.three.digit.algorithm.IAlgorithm#doCalculate()
     */
    public void doCalculate ()
    {
        Set <String> keys = iGroup.keySet ();

        Iterator <String> iterator = keys.iterator ();
        while (iterator.hasNext ())
        {
            String key = iterator.next ();

            List <Point> itemList = iGroup.getGroup (key);
            PercentHashSet <Integer> serial = new PercentHashSet <Integer> ();

            for (int i = 0, ii = itemList.size (); i < ii; i++)
            {
                Point p1 = itemList.get (i);
                Point p2 = Utils.next (p1, HIS_LIST);

                if (p2 != null)
                {
                    serial.add (math (p1, p2));
                }
            }

            serials.put (key, serial);
        }

    }

    private int math (Point p1, Point p2)
    {
        double sum = Math.pow ((p1.getX () - p2.getX ()), 2) + Math.pow ((p1.getY () - p2.getY ()), 2)
                     + Math.pow ((p1.getZ () - p2.getZ ()), 2);

        return (int) sum;
    }

    public void print ()
    {
        Set <String> groupKey = serials.keySet ();
        Iterator <String> iterator = groupKey.iterator ();
        while (iterator.hasNext ())
        {
            PercentHashSet <Integer> serial = serials.get (iterator.next ());
            serial.print ();
            System.out.println ("size: " + serial.size ());
            System.out.println ("=---------------------------=");
        }
    }

    public void print (String group)
    {
        PercentHashSet <Integer> serial = getSerial (group);
        if (serial != null)
        {
            serial.print ();
            System.out.println ("size: " + serial.size ());
        }
    }

    public PercentHashSet <Integer> getSerial (String group)
    {
        return serials.get (group);
    }

    public List <Point> doFilter (Point base, PercentHashSet <Integer> serial)
    {
        List <Point> rtn = new ArrayList <Point> ();

        for (int i = 0; i < BASE_LIST.size (); i++)
        {
            Point point = BASE_LIST.get (i);

            if (serial.contains (math (base, point)))
            {
                rtn.add (point);
            }
        }
        return rtn;
    }
}
